package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bean.Student;

public class StudentDao extends DaoUtil{
	
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null;
	
	//注册添加学生
	public boolean addStu(Student stu){
		boolean b = false;
		conn = this.getConnection();
		String sql = "insert into stuUserInfo (stuUserID,stuName,belongclass,stuEmail,major) values (?,?,?,?,?);";
		String sql2 = "insert into stuLogin (stuUserID,stuName,stuPassword) values (?,?,?);";
		//System.out.println(sql);
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, stu.getStuUserID());
			stat.setString(2, stu.getStuName());
			stat.setString(3, stu.getBelongclass());
			stat.setString(4, stu.getStuEmail());
			stat.setString(5, stu.getMajor());
			stat.execute();
			stat = conn.prepareStatement(sql2);
			stat.setInt(1, stu.getStuUserID());
			stat.setString(2, stu.getStuName());
			stat.setString(3, stu.getStuPassword());
			return stat.execute();
			
		} catch (SQLException e) {
			e.printStackTrace();
			return b;
		}finally{
			this.closed();
		}
	}
	//注册查询
	public boolean testStu(Student stu){
		conn = this.getConnection();
		String sql = "select * from stuLogin where stuUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1,stu.getStuUserID());
			rs = stat.executeQuery();
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally{
			closed();
		}
	
		return false;
	}
	
	
	//登录查询
	public Student findStu(Student stu){
		conn = this.getConnection();
		String sql = "select * from stuLogin where stuUserID = ? and stuPassword = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1,stu.getStuUserID());
			stat.setString(2, stu.getStuPassword().trim());
			rs = stat.executeQuery();
			if(rs.next()){
				return stu;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			closed();
		}
	
		return null;
	}
	
	public int updateP(Student stu ,String newP){
		conn = this.getConnection();
		String sql = "UPDATE stuLogin SET stuPassword = ? WHERE stuUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setString(1,newP);
			stat.setInt(2, stu.getStuUserID());
			return stat.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return 0;
		}finally {
			closed();
		}
		
	}
	//查找学生信息
	public Student findStuInfo(int stuUserID){
		conn = this.getConnection();
		Student stu = new Student();
		//List list = new ArrayList();
		String sql = "select * from stuuserinfo where stuUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, stuUserID);
			rs = stat.executeQuery();
			while (rs.next()) {				
				stu.setStuUserID(rs.getInt(1));
				stu.setStuName(rs.getString(2));
				stu.setBelongclass(rs.getString(3));
				stu.setStuEmail(rs.getString(4));
				stu.setMajor(rs.getString(5));
				System.out.println(rs.getInt(1));
				System.out.println(rs.getString(2));
				System.out.println("dfdfdf");
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		} finally {
			closed();
		}
		return stu;		
	}
	//更新学生信息
	public boolean updatestuself(Student stu){
		boolean b = false;
		conn = this.getConnection();
		/*Student stu2 = (Student) ServletActionContext.getRequest().getSession().getAttribute("stu");
		int stuid = stu2.getStuUserID();*/
		String sql = "update stuuserinfo set stuName=?,belongclass=?,stuEmail=?,major=? where stuUserID=?";
		String sql2 = "update stulogin set stuName=? where stuUserID=?";
		//System.out.println(sql);
		try {
			stat = conn.prepareStatement(sql);
			
			stat.setString(1, stu.getStuName());
			stat.setString(2, stu.getBelongclass());
			stat.setString(3, stu.getStuEmail());
			stat.setString(4, stu.getMajor());
			stat.setInt(5, stu.getStuUserID());
			
			stat.execute();
			stat = conn.prepareStatement(sql2);
			stat.setString(1, stu.getStuName());
			stat.setInt(2, stu.getStuUserID());
			b=stat.execute();
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}finally{
			this.closed();
		}
		return b;
	}
}
